﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using System.Reflection;

namespace Bridges.Model
{
   public class Util
    {
        /// <summary>
        /// Método que calcula Hash dinâmico
        /// </summary>
        /// <param name="input">valor</param>
        /// <returns></returns>
        public static String CalcularHash(String input)
        {
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            StringBuilder str = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                str.Append(hash[i].ToString("X2"));
            }
            return str.ToString();
        }

        /// <summary>
        /// Método que gera numero aleatorio
        /// </summary>
        /// <returns></returns>
        public static int GerarNroAleatorio()
        {
            Random rnd = new Random();
            return rnd.Next();
        }


        public static DataTable ConverteListParaDataTable<T>(List<T> list)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
            }
            foreach (T t in list)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo info in typeof(T).GetProperties())
                {
                    row[info.Name] = info.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }
            return dt;
        }

        public static void none() { }
    }
}
